package hotel.base.oversea.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lmf
 * @Date 2020/8/17 14:12
 * execl操作类
 **/
@Slf4j
public class ExcelUtil {
    public static List<Object[]> importExcel(InputStream inputStream){
        List<Object[]> list=new ArrayList<>();
        try {
            Workbook workbook= WorkbookFactory.create(inputStream);
            Sheet sheet=workbook.getSheetAt(0);
            //获取sheet的行数
            int rows=sheet.getPhysicalNumberOfRows();
            for(int i=0;i<rows;i++){
                //表头不做处理
                if(i==0){
                    continue;
                }
                //获取当前行的数据
                Row row=sheet.getRow(i);
                Object[] objects=new Object[row.getPhysicalNumberOfCells()];
                int index=0;
                for(Cell cell:row){
                    objects[index]=cell.getStringCellValue();
                    index++;
                }
                list.add(objects);
            }
            log.info("=============导入文件解析成功!===========");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("=============导入文件解析失败!===========");
        }
        return list;
    }
}
